﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.IO;
using Common;
using Model;
using DAL;
using System.Drawing.Imaging;

namespace UI.tools
{
    /// <summary>
    /// Upload 的摘要说明
    /// </summary>
    public class Upload : IHttpHandler, IRequiresSessionState
    {
        HttpContext content;
        public int id;
        public int adminid;
        public void ProcessRequest(HttpContext context)
        {
            content = context;
            string handle = context.Request["handle"];
            string ids = content.Request["id"];
            int num = DataConvert.ToInt(content.Request["num"]);
            if (ids != "undefined")
            {
                id = DataConvert.ToInt(content.Request["id"]);
            }
            if (B.MyAdmin != null) 
            {
                adminid = B.MyAdmin.ID;
            }
            
            context.Response.ContentType = "text/plain";
            if (handle == "upload_img") //上传产品图片
            {
                string path = UploadImg();
                context.Response.Write(path);
            }
            if (handle == "upload_file") //上传附件
            {
                string path = UploadFile();
                context.Response.Write(path);
            }
            if (handle == "upload_annex") //上传附件
            {
                string path = UploadImgs(1);
                context.Response.Write(path);
            }
            if (handle == "upload_annex_jp") //借片上传
            {
                string path = UploadImgs(2);
                path+="|"+num;
                context.Response.Write(path);
            }
            if (handle == "upload_annex_hp") //还片上传
            {
                string path = UploadImgs(3);
                path += "|" + num;
                context.Response.Write(path);
            }
            if (handle == "upload_annex_ty") //还上传
            {
                string path = UploadImg();
                context.Response.Write(path);
            }
            if (handle == "upload_excel")
            {
                string path = UploadExcel();
                context.Response.Write(path);
            }
            if (handle == "upload_excel2")
            {
                string path = UploadExcel2();
                context.Response.Write(path);
            }

        }
        public string UploadImg()
        {
            string random = JobRequest.GetRequestString("random");
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            List<string> filelist = new List<string>();
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string file = rootpath + uploadFile.FileName;
                    uploadFile.SaveAs(file);
                    filelist.Add(opath + uploadFile.FileName);
                }                
            }

            return SerializeHelper.ToJsonByDynamic(filelist);
        }
        public string UploadImgs(int nums)
        {
            string random = JobRequest.GetRequestString("random");
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + DateTime.Now.ToLongTimeString() + "/";
            string pa = opath.Replace(':','-');
            var rootpath = content.Server.MapPath(pa);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            List<string> filelist = new List<string>();
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string file = rootpath + uploadFile.FileName;
                    uploadFile.SaveAs(file);
                    filelist.Add(opath + uploadFile.FileName);
                }
            }
            string paths = SerializeHelper.ToJsonByDynamic(filelist);
            string yinhao = '"'.ToString();
            string paths1 = paths.Replace(yinhao, "");
            string paths2 = paths1.Replace("[", "");
            string paths3 = paths2.Replace("]", "");
            string paths4 = paths3.Replace(':', '-');

            string path = paths4;
            if (id > 0) {
                if (nums == 1)
                {
                    RukuDAL.UpdateImgs(path,id);
                }
                if (nums == 2)
                {
                    JiepianDAL.UpdateImgs(path,id);
                }
                if (nums == 3)
                {
                    HuanpianDAL.UpdateImgs(path,id);
                }
            }
            List<string> filelists = new List<string>();
            filelists.Add(path);
            return SerializeHelper.ToJsonByDynamic(filelists);
        }
        public string UploadFile()
        {
            var content = HttpContext.Current;
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string path = "";
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    path = opath + unique + ext + "*" + uploadFile.FileName;
                }
            }
            return path;
        }
        public string UploadExcel()
        {
            string random = JobRequest.GetRequestString("random");
            string opath = "/upload/excel/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            List<string> filelist = new List<string>();
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                string ext = Path.GetExtension(uploadFile.FileName);
                
                if (uploadFile.ContentLength > 0)
                {
                    string filename = "mobile"+adminid+"" + ext;
                    string file = rootpath + filename;
                    uploadFile.SaveAs(file);
                    filelist.Add(opath + filename);
                }

            }
            return SerializeHelper.ToJsonByDynamic(filelist);
        }
        public string UploadExcel2()
        {
            string random = JobRequest.GetRequestString("random");
            string opath = "/upload/excel/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            List<string> filelist = new List<string>();
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                string ext = Path.GetExtension(uploadFile.FileName);

                if (uploadFile.ContentLength > 0)
                {
                    //string filename = "mobiles" + adminid + "" + ext;
                    string filename = "mobiles" + adminid + ".xlsx";
                    string file = rootpath + filename;
                    uploadFile.SaveAs(file);
                    filelist.Add(opath + filename);
                }

            }
            return SerializeHelper.ToJsonByDynamic(filelist);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        //public string UploadAnnex(int userid, string itemname)
        //{
        //    string random = JobRequest.GetRequestString("random");
        //    string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
        //    var rootpath = content.Server.MapPath(opath);
        //    if (!System.IO.Directory.Exists(rootpath))
        //    {
        //        Directory.CreateDirectory(rootpath);
        //    }
        //    string path = "";
        //    for (int j = 0; j < content.Request.Files.Count; j++)
        //    {
        //        HttpPostedFile uploadFile = content.Request.Files[j];
        //        if (uploadFile.ContentLength > 0)
        //        {
        //            string ext = Path.GetExtension(uploadFile.FileName);
        //            string unique = DateTime.Now.ToString("HHmmssfff");
        //            string file = rootpath + unique + ext;
        //            string name = file.Substring(0, file.LastIndexOf('.'));
        //            uploadFile.SaveAs(file);
        //            AnnexInfo info = new AnnexInfo()
        //            {
        //                UserID = userid,
        //                //ItemName = itemname,
        //                Name = uploadFile.FileName,
        //                Path = opath + unique + ext,
        //                CreateDate = DateTime.Now
        //            };
        //            int ID = AnnexDAL.Save(info);
        //            path = ID + "*" + opath + unique + ext;
        //        }

        //    }
        //    return path;
        //}
    }
}